package com.whut.transportation;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.whut.transportation.entity.Message;
import com.whut.transportation.entity.TaskInfo;
import com.whut.transportation.entity.TransInfo;
import com.whut.transportation.network.HttpManager;
import com.whut.transportation.network.HttpRequestVo;
import com.whut.transportation.parser.JSONParser;
import com.whut.transportation.ui.BaseActivity;
import com.whut.transportation.utils.Constant;
import com.whut.transportation.utils.MyLog;
import com.whut.transportation.wheel.StrericWheelAdapter;
import com.whut.transportation.wheel.WheelView;

/**
 * @author
 */
public class EntruckDetailActivity extends BaseActivity {
	private TextView entruck_number;
	private TextView entruck_trainNum;
	private TextView entruck_train;
	private TextView entruck_station;
	private TextView entruck_goods;
	private TextView entruck_name;
	private TextView entruck_weight;
	private TextView entruck_gudao;
	private TextView entruck_time;
	private Button entruck_xiugai;

	private EditText entruck_bu_number;
	private Button entruck_submit;

	private TransInfo transinfo;
	private TaskInfo taskInfo;

	private SharedPreferences preferences;
	private boolean flag = false;
	private String gudao;

	private int minYear = 1970; // 最小年份
	private int fontSize = 13; // 字体大小
	private WheelView yearWheel, monthWheel, dayWheel, hourWheel, minuteWheel;
	public static String[] yearContent = null;
	public static String[] monthContent = null;
	public static String[] dayContent = null;
	public static String[] hourContent = null;
	public static String[] minuteContent = null;

	@Override
	protected void loadViewLayout() {
		setContentView(R.layout.activity_entruck_detail);
		Intent intent = getIntent();
		transinfo = (TransInfo) intent.getSerializableExtra("transinfo");
		preferences = getSharedPreferences("userId", Context.MODE_PRIVATE);
		setTitleBarView(true, transinfo.getTrainNumber() + transinfo.getTrain()+ "装车", -1, true);
		initContent();
	}

	@Override
	protected void findViewById() {
		entruck_number = (TextView) findViewById(R.id.entruck_number);
		entruck_trainNum = (TextView) findViewById(R.id.entruck_trainNum);
		entruck_train = (TextView) findViewById(R.id.entruck_train);
		entruck_station = (TextView) findViewById(R.id.entruck_station);
		entruck_goods = (TextView) findViewById(R.id.entruck_goods);
		entruck_name = (TextView) findViewById(R.id.entruck_name);
		entruck_weight = (TextView) findViewById(R.id.entruck_weight);
		entruck_gudao = (TextView) findViewById(R.id.entruck_gudao);
		entruck_time = (TextView) findViewById(R.id.entruck_time);
		entruck_xiugai = (Button) findViewById(R.id.entruck_xiugai);

		entruck_bu_number = (EditText) findViewById(R.id.entruck_bu_number);
		entruck_submit = (Button) findViewById(R.id.entruck_submit);
	}

	@Override
	protected void afterFindViewId() {
		entruck_number.setText(entruck_number.getText() + transinfo.getNumber());
		entruck_trainNum.setText(entruck_trainNum.getText()+ transinfo.getTrainNumber());
		entruck_train.setText(entruck_train.getText() + transinfo.getTrain());
		entruck_station.setText(entruck_station.getText()+ transinfo.getStation());
		entruck_goods.setText(entruck_goods.getText() + transinfo.getGoods());
		entruck_name.setText(entruck_name.getText() + transinfo.getName());
		entruck_weight.setText(entruck_weight.getText()+ String.valueOf(transinfo.getWeight()));
		entruck_gudao.setText(entruck_gudao.getText()+ String.valueOf(transinfo.getGudao()));
		
	}

	@Override
	protected void processLogic() {
		HttpRequestVo vo = new HttpRequestVo();
		vo.requestUrl = Constant.Find_TASK_INFO;
		vo.context = mContext;
		HashMap<String, String> req = new HashMap<String, String>();
		req.put("account", preferences.getString("account", null));
		req.put("id", transinfo.getId());
		vo.requestDataMap = req;
		vo.parser = new JSONParser(TaskInfo.class);
		getDataFromServer(HttpManager.POST_MOTHOD, vo,
				new DataCallback<HashMap<String, Object>>() {
					@Override
					public void onStart() {
					}
					@Override
					public void onFailed() {
						showNetErrorDialog();
					}
					@Override
					public void processData(
							HashMap<String, Object> paramObject,
							boolean paramBoolean) {
						taskInfo = (TaskInfo) paramObject.get("result");
						entruck_bu_number.setText(taskInfo.getBunum());
						
						if (taskInfo.getIntime()!=null) {
							entruck_time.setText(taskInfo.getIntime());
						} else {
							entruck_time.setText(transinfo.getTime());
						}
					}
					@Override
					public void onFinish() {
						closeProgressDialog();
					}
				});
	}

	@Override
	protected void setListener() {
		entruck_xiugai.setOnClickListener(this);
		entruck_submit.setOnClickListener(this);
	}

	@Override
	protected void getDataAgain() {
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.entruck_submit:
			submitEntruck();
			break;
		case R.id.entruck_xiugai:
			show5();
			break;
		default:
			break;
		}
		super.onClick(v);
	}

	private void show5() {
		View view = ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE))
				.inflate(R.layout.time_picker, null);
		Calendar calendar = Calendar.getInstance();
		int curYear = calendar.get(Calendar.YEAR);
		int curMonth = calendar.get(Calendar.MONTH) + 1;
		int curDay = calendar.get(Calendar.DAY_OF_MONTH);
		int curHour = calendar.get(Calendar.HOUR_OF_DAY);
		int curMinute = calendar.get(Calendar.MINUTE);
		int curSecond = calendar.get(Calendar.SECOND);

		yearWheel = (WheelView) view.findViewById(R.id.yearwheel);
		monthWheel = (WheelView) view.findViewById(R.id.monthwheel);
		dayWheel = (WheelView) view.findViewById(R.id.daywheel);
	/*	hourWheel = (WheelView) view.findViewById(R.id.hourwheel);
		minuteWheel = (WheelView) view.findViewById(R.id.minutewheel);*/

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setView(view);

		yearWheel.setAdapter(new StrericWheelAdapter(yearContent));
		yearWheel.setCurrentItem(curYear - 2013);
		yearWheel.setCyclic(true);
		yearWheel.setInterpolator(new AnticipateOvershootInterpolator());

		monthWheel.setAdapter(new StrericWheelAdapter(monthContent));

		monthWheel.setCurrentItem(curMonth - 1);

		monthWheel.setCyclic(true);
		monthWheel.setInterpolator(new AnticipateOvershootInterpolator());

		dayWheel.setAdapter(new StrericWheelAdapter(dayContent));
		dayWheel.setCurrentItem(curDay - 1);
		dayWheel.setCyclic(true);
		dayWheel.setInterpolator(new AnticipateOvershootInterpolator());

		hourWheel.setAdapter(new StrericWheelAdapter(hourContent));
		hourWheel.setCurrentItem(curHour);
		hourWheel.setCyclic(true);
		hourWheel.setInterpolator(new AnticipateOvershootInterpolator());

		minuteWheel.setAdapter(new StrericWheelAdapter(minuteContent));
		minuteWheel.setCurrentItem(curMinute);
		minuteWheel.setCyclic(true);
		minuteWheel.setInterpolator(new AnticipateOvershootInterpolator());

		builder.setTitle("选取时间");
		builder.setPositiveButton("确  定",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {

						StringBuffer sb = new StringBuffer();
						sb.append(yearWheel.getCurrentItemValue()).append("-")
								.append(monthWheel.getCurrentItemValue())
								.append("-")
								.append(dayWheel.getCurrentItemValue());

						sb.append(" ");
						sb.append(hourWheel.getCurrentItemValue()).append(":")
								.append(minuteWheel.getCurrentItemValue());
						MyLog.i("time", sb.toString() + "--------");
						entruck_time.setText(sb.toString());
						dialog.cancel();
					}
				});

		builder.show();
	}

	private void show4() {
		String bu_number = entruck_bu_number.getText().toString();
		String in_time = entruck_time.getText().toString();
		if(0!=bu_number.length()){
			updateTaskInfo(in_time, bu_number);
		} else {
			Toast.makeText(mContext, "施封篷布号不能为空", Toast.LENGTH_SHORT).show();
		}
	}
	private void submitEntruck() {
		 AlertDialog.Builder builder = new AlertDialog.Builder(EntruckDetailActivity.this);
		 builder.setTitle("提醒");
		 builder.setMessage("确定提交吗？");
		 builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
				String bu_number = entruck_bu_number.getText().toString();
				// 如果送入时间已修改，则在这要用新的时间替代，需要查询得到****
				// String in_time = transinfo.getTime();
				String in_time = entruck_time.getText().toString().trim();

				if (bu_number.length() == 0)
					Toast.makeText(mContext, "施封蓬布号不能为空", Toast.LENGTH_SHORT);
				else
					updateTaskInfo(in_time, bu_number);
			}
		 });
		 builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
             @Override
             public void onClick(DialogInterface dialog, int which)
             {
             }
         });
		 builder.show();
	}

	// 更新TaskInfo
	private void updateTaskInfo(String in_time,String bu_number) {
		HttpRequestVo vo = new HttpRequestVo();
		vo.requestUrl = Constant.UPDATE_TASK_INFO;
		vo.context = mContext;
		vo.parser = new JSONParser(Message.class);
		HashMap<String, String> req = new HashMap<String, String>();
		req.put("account", preferences.getString("account", null));
		req.put("id", transinfo.getId());
		req.put("taskStyle", String.valueOf(transinfo.getType()));
		req.put("shunhao", transinfo.getNumber());
		if (null != in_time){
			req.put("taskInfo.timeIn", in_time);
			taskInfo.setIntime(in_time);
		}
		if (null != bu_number){
			req.put("taskInfo.tarpaulinId", bu_number);
			taskInfo.setBunum(bu_number);
		}
		vo.requestDataMap = req;
		getDataFromServer(HttpManager.POST_MOTHOD, vo,
				new DataCallback<HashMap<String, Object>>() {
					@Override
					public void onStart() {
						showProgressDialog(getResources().getString(
								R.string.loading_content));
					}
					@Override
					public void onFailed() {
						showNetErrorDialog();
					}
					@Override
					public void processData(
							HashMap<String, Object> paramObject,
							boolean paramBoolean) {
						Toast.makeText(mContext, "提交成功", Toast.LENGTH_SHORT).show();
						Message message = (Message) paramObject.get("result");
						if (message.getMessage() == 1) {
							Intent intent = new Intent(EntruckDetailActivity.this,SuccessActivity.class);
							intent.putExtra("transinfo", transinfo);
							intent.putExtra("taskInfo", taskInfo);
							startActivity(intent);
							finish();
						}
					}
					@Override
					public void onFinish() {
						closeProgressDialog();
					}
				});
	}

	public void initContent() {
		yearContent = new String[10];
		for (int i = 0; i < 10; i++)
			yearContent[i] = String.valueOf(i + 2013);

		monthContent = new String[12];
		for (int i = 0; i < 12; i++) {
			monthContent[i] = String.valueOf(i + 1);
			if (monthContent[i].length() < 2) {
				monthContent[i] = "0" + monthContent[i];
			}
		}

		dayContent = new String[31];
		for (int i = 0; i < 31; i++) {
			dayContent[i] = String.valueOf(i + 1);
			if (dayContent[i].length() < 2) {
				dayContent[i] = "0" + dayContent[i];
			}
		}
		hourContent = new String[24];
		for (int i = 0; i < 24; i++) {
			hourContent[i] = String.valueOf(i);
			if (hourContent[i].length() < 2) {
				hourContent[i] = "0" + hourContent[i];
			}
		}

		minuteContent = new String[60];
		for (int i = 0; i < 60; i++) {
			minuteContent[i] = String.valueOf(i);
			if (minuteContent[i].length() < 2) {
				minuteContent[i] = "0" + minuteContent[i];
			}
		}
	}
}